module AList (
AList, del, get, get1, isIn, isNil, nil, put, testAlist

) where

-- nil, put, isIn, get, del, isNil

type AList a = [(Char,a)]

get :: AList a -> Char -> Maybe a
get alist c =
  case filter ((== c) . fst) alist of
    -- get alist c = filter (\p-> fst p == c) alist
    -- get alist c = filter (\(c1,x) -> c1 == c) alist
    []       -> Nothing
    (_,x):_ -> Just x

get1 :: AList a -> Char -> a
get1 alist c =
  case filter ((== c) . fst) alist of
    [] -> error $ show c ++ " not in alist"
    (_,x):_ -> x

del :: AList a -> Char -> AList a
del alist c = filter ((/= c) . fst) alist

put :: AList a -> Char -> a -> AList a
put alist c x = (c,x) : del alist c

-- warning?
nil :: AList a
nil = []

isIn :: AList a -> Char -> Bool
isIn alist c =
        case get alist c of
                Nothing -> False
                Just _ -> True

-- 3 Fall im Case?            
isNil :: AList a -> Bool
isNil alist = 
        case alist of
                []      -> True
                (_,_):_ -> False 

-- Testlists:
testAlist :: AList Int                            
testAlist = zip ['x', 'y', 'z'] [1,2,3]